package cn.fz.schedule;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.time.LocalTime;

/**
 * 预定日程生产者
 */
public class ScheduleProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("ScheduleProducer");
        producer.setNamesrvAddr("localhost:9876");
        producer.start();
        for (int i = 0; i < 10; i++) {
            Message message = new Message("Schedule", "Tags", ("Hello ScheduleProducer " + i).getBytes(StandardCharsets.UTF_8));
            // 1到18分别对应messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
            // message.setDelayTimeLevel(2);
            // 10秒后发送,指定日期,(也可以是当前时间加上10秒)
            message.setDelayTimeMs(10000L);
            producer.send(message);
            System.out.println("消息发送成功-" + LocalTime.now());
        }
        producer.shutdown();
    }
}
